#include <gfx/buffer.h>

inka::gfx::Buffer::Buffer(GLenum type)
  : _type(type)
{
  glGenBuffers (1, &_name);
}

namespace
{
GLenum convert_access_usage (inka::gfx::BufferAccess access, inka::gfx::BufferUsage usage)
{
  GLenum result = GL_STATIC_DRAW;
  switch (usage)
    {
    case inka::gfx::BU_Dynamic:
      result = GL_DYNAMIC_DRAW;
      break;
    case inka::gfx::BU_Static:
      result = GL_STATIC_DRAW;
      break;
    case inka::gfx::BU_Stream:
      result = GL_STREAM_DRAW;
      break;
    }
  switch (access)
    {
    case inka::gfx::BA_Draw:
      result += 0;
      break;
    case inka::gfx::BA_Read:
      result += 1;
      break;
    case inka::gfx::BA_Copy:
      result += 2;
    }

  return result;
}
}

void inka::gfx::Buffer::SetData (const void *data, unsigned size, BufferAccess access, BufferUsage usage)
{
  Bind ();

  glBufferData (_type, size, data, convert_access_usage(access, usage));
}

void inka::gfx::Buffer::SetSubData (unsigned offset, const void *data, unsigned size)
{
  Bind ();

  glBufferSubData (_type, offset, size, data);
}

void inka::gfx::Buffer::Bind()
{
  glBindBuffer(_type, _name);
}
